/**
 用到的listviewItem 是lmy_all_books_item.xml
 */
package org.buptsse.ui;

import java.util.LinkedList;

import org.buptsse.entity.SelledBookItem;
import org.buptsse.thread.GetSelledBookListThread;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.GloableInstance;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MyAllBooksActivity extends ListActivity implements OnScrollListener,OnClickListener{

	ListAdapter listAdapter = new ListAdapter();
	ListView list;
	int visibleItemCount;
	
	int scrollState;
	LinkedList<SelledBookItem> resultList = new LinkedList<SelledBookItem>();
	int lastItem;
	int count;
	private Button backButton,refreshButton,publishNewBookButton;
	private ProgressDialog pDialog;
	private TextView priceTV,stateTV,bookNameTV,sellTimeTV,lastUpdateTimeTV,endTimeTV;

	Handler myHandler = new Handler() {
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case APPConstant.NetUnavailable:
				Toast.makeText(getApplicationContext(), "网络连接不可用", 1000).show();
				break;
			case APPConstant.getSelledBookListError:
				Toast.makeText(getApplicationContext(), "获取内容失败", 1000).show();
				break;
			case APPConstant.getSelledBookListFinish:
				resultList=(LinkedList<SelledBookItem>) msg.obj;
				listAdapter.count=resultList.size();
				listAdapter.notifyDataSetChanged();
				break;
			}
			pDialog.dismiss();
			super.handleMessage(msg);
		}
	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lmy_all_books);
		backButton=(Button) findViewById(R.id.rd_back);
		backButton.setOnClickListener(this);
		refreshButton=(Button) findViewById(R.id.rd_refresh);
		refreshButton.setOnClickListener(this);
		publishNewBookButton=(Button) findViewById(R.id.rd_publish_new_book);
		publishNewBookButton.setOnClickListener(this);
		listAdapter.count=0;
		setListAdapter(listAdapter);
		listAdapter.notifyDataSetChanged();
		getSelledBookList();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent intent=new Intent();
		intent.putExtra("sellid", resultList.get(position).getSellid());
		intent.setClass(MyAllBooksActivity.this, ManageBookActivity.class);
		startActivity(intent);
	}

	private void getSelledBookList()
	{
		GetSelledBookListThread gsblt=new GetSelledBookListThread(getApplicationContext(), myHandler, GloableInstance.user.getName());
		gsblt.start();
		ShowWaittingDialog("正在加载内容");
	}
	
	public void ShowWaittingDialog(String msg)
	{
		pDialog=new ProgressDialog(this);
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setMessage(msg); 
        pDialog.show(); 
	}
	class ListAdapter extends BaseAdapter {
		int count = 10;
		@Override
		public int getCount() {
			return count;
		}
		@Override
		public Object getItem(int position) {
			return position;
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(MyAllBooksActivity.this);
			View view = inflater.inflate(R.layout.lmy_all_books_item, null);
			priceTV=(TextView) view.findViewById(R.id.price);
			stateTV=(TextView) view.findViewById(R.id.state);
			bookNameTV=(TextView) view.findViewById(R.id.bookname);
			sellTimeTV=(TextView) view.findViewById(R.id.selltime);
			lastUpdateTimeTV=(TextView) view.findViewById(R.id.last_update_time);
			endTimeTV=(TextView) view.findViewById(R.id.endtime);
			SelledBookItem item=resultList.get(position);
			priceTV.setText(item.getPrice());
			stateTV.setText(item.getState());
			bookNameTV.setText(item.getBookname());
			sellTimeTV.setText(item.getSelltime());
			lastUpdateTimeTV.setText(item.getLast_update_time());
			endTimeTV.setText(item.getEndtime());
			return view;
		}
	}
	
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		this.visibleItemCount = visibleItemCount;
		lastItem = firstVisibleItem + visibleItemCount - 1;
		if (listAdapter.count >= count) {
			list.removeFooterView(view);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		this.scrollState = scrollState;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.rd_back:
			finish();
			break;
		case R.id.refresh_button:
			getSelledBookList();
			break;
		case R.id.rd_publish_new_book:
			Intent intent=new Intent();
			intent.setClass(MyAllBooksActivity.this, PostNewBookActivity.class);
			startActivity(intent);
			finish();
			break;
		}
	}

}
